package com.binfenhulian.chinaenergy.ces.manage.controller.account;

import com.binfenhulian.chinaenergy.ces.core.bean.result.AjaxResult;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.AccountParam;
import com.binfenhulian.chinaenergy.ces.manage.service.account.AccountService;
import com.binfenhulian.chinaenergy.ces.utils.string.StringUtils;
import com.github.pagehelper.PageInfo;
import com.binfenhulian.chinaenergy.ces.manage.bean.param.PageParam;
import com.binfenhulian.chinaenergy.ces.manage.core.db.po.def.AccountPO;
import com.binfenhulian.chinaenergy.ces.manage.framework.interceptor.ManageLoginCheck;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * 管理员控制器
 * @author liuyang
 */

@ManageLoginCheck
@Controller
@RequestMapping("/manage/account")
public class AccountController {

    @Resource(name = "com.binfenhulian.chinaenergy.ces.manage.service.account.AccountServiceImpl")
    AccountService accountService;

    @RequestMapping("/list.html")
    public String listAccount(HttpServletRequest request, HttpServletResponse response) {
        return "manage/account";
    }

    @RequestMapping("/add")
    @ResponseBody
    public Object addAccount(HttpServletRequest request, HttpServletResponse response,AccountParam accountParam) {
        String name = accountParam.getName();
        String password = accountParam.getPassword();
        String mobile = accountParam.getPassword();

        if (StringUtils.isEmpty(name)) {
            return new AjaxResult(0, "姓名字段不能为空", false, null);
        }

        AccountPO accountByMobile = accountService.getAccountByMobile(mobile);
        if (accountByMobile != null) {
            return new AjaxResult(0, "手机号已存在", false, null);
        }

        if (StringUtils.isEmpty(password) || password.length() < 6) {
            return new AjaxResult(0, "密码不能小于6位", false, null);
        }

        // 添加
        int accountId = accountService.addAccount(name, password, mobile);
        return new AjaxResult(0, "成功", true, accountId);
    }


    @RequestMapping("/list")
    @ResponseBody
    public Object accountList(HttpServletRequest request,
                              HttpServletResponse response,
                              PageParam pageParam) {
        PageInfo page = accountService.listAccountByPage(pageParam);
        return new AjaxResult(0, "查找成功", true, page);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Object accountList(HttpServletRequest request,
                              HttpServletResponse response,
                              int id) {
        accountService.deleteAccountById(id);
        return new AjaxResult(0, "删除成功", true, null);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object accountList(HttpServletRequest request,
                              HttpServletResponse response,
                              AccountPO accountPO) {

        if (accountPO.getId() == null) {
            return new AjaxResult(0, "缺少账号ID", false, null);
        }

        // 不更新密码
        accountPO.setPassword(null);
        accountService.updateAccount(accountPO);
        return new AjaxResult(0, "更新成功", true, null);
    }
}
